package com.yiqi.liebang.feature.mine.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.QuestionBo;
import com.yiqi.liebang.feature.home.contract.QuestionContract;
import com.yiqi.liebang.feature.home.dependencies.question.DaggerQuestionComponent;
import com.yiqi.liebang.feature.home.dependencies.question.QuestionPresenterModule;
import com.yiqi.liebang.feature.home.view.AskDetailActivity;
import com.yiqi.liebang.feature.mine.view.adapter.OtherQuestionAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OtherTopicListActivity extends BaseActivity implements QuestionContract.View, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {


    @BindView(R.id.rv_search)
    RecyclerView mRvSearch;

    @Inject
    QuestionContract.Presenter mPresenter;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    private OtherQuestionAdapter mAdapter;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<QuestionBo> mData;

    private String mType;

    private String mUserUid;


    @Override
    public int attachLayoutRes() {
        return R.layout.activity_other_topic_list;
    }

    @Override
    protected void initInjector() {
        DaggerQuestionComponent.builder().questionPresenterModule(new QuestionPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {

        initAdapter();
        initToolBar(mToolbar,"问答列表");
    }

    public void initAdapter() {
        mEmptyView = new EmptyView(mRvSearch);
        mRvSearch.setLayoutManager(new LinearLayoutManager(this));
        mRvSearch.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter = new OtherQuestionAdapter();
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapter.bindToRecyclerView(mRvSearch);
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
        mAdapter.setOnLoadMoreListener(this, mRvSearch);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
    }


    @Override
    protected void initData() {
        mType = this.getIntent().getStringExtra("type");
        mUserUid = this.getIntent().getStringExtra("id");
        mPresenter.getOtherQuestion(mType, mUserUid, pageIndex, pageSize);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void showQuestionListSuccess(List<QuestionBo> workBoPageData) {
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    private void loadMore() {
        mPresenter.getOtherQuestion(mType, mUserUid, pageIndex, pageSize);
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showQuestionListError(String error) {
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showQuestionListMoreSuccess(List<QuestionBo> msg) {
        setData(false, msg);
    }

    @Override
    public void showQuestionListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showQuestionListEmpty() {
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, AskDetailActivity.class);
        intent.putExtra("answer_id", mAdapter.getData().get(position).getId());
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (mAdapter.getData().get(position).getDataPrivacyType() == 0) {
            Intent intent = new Intent(this, UserInfoActivity.class);
            intent.putExtra("is_view_my", false);
            intent.putExtra("visitorUid", mAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
            startActivity(intent);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
